<?php
/**
 * Created by PhpStorm.
 * User: alice
 * Date: 2015/12/28
 * Time: 12:26
 */

/**
 * Class Involved_CartModel
 */
class Involved_CartModel extends BaseModel{

    protected $table = 'cart';


    /**
     * 获取购物车列表
     * @param $user_id
     * @return mixed
     */
//    public function GetUserCartList($user_id,$paramObj)
//    {
//        $offset = ($paramObj->page-1) * HostEnum::PAGE_NUM;
//        return  DB::table($this->table)
//                ->select('cart.cart_id','cart.supplier_id','cart.supplier_name','cart.product_id','cart.product_name','cart.product_img','cart.spec_name','cart.price','cart.number','product.stock','cart.place','cart.storage','cart.unit')
//                ->leftjoin('product','product.product_id', '=', 'cart.product_id')
//                ->where('user_id',$user_id)
//                ->skip($offset)->take(HostEnum::PAGE_NUM)
//                ->orderBy('cart.created','desc')
//                ->get();
//    }

    /**
     * 购物车和
     * @param $user_id
     * @return mixed
     */
    public function GetShopUserCartSum($user_id)
    {
        return DB::select("select sum(sc_product.product_price*sc_cart.number*sc_product.piece_weight) as price,sum(sc_product.piece_weight*sc_cart.number) as weight,sum(sc_cart.number) as num from sc_cart left join sc_product on sc_cart.product_id=sc_product.product_id where user_id = $user_id");
    }

    /**
     * 购物车提交商品求和
     * @param $cart_id
     * @return mixed
     */
    public function ShopSubmitCartSum($cart_id)
    {
        return DB::select("select sum(sc_product.product_price*sc_cart.number*sc_product.piece_weight) as price,sum(sc_product.piece_weight*sc_cart.number) as weight,sum(sc_cart.number) as num from sc_cart left join sc_product on sc_cart.product_id=sc_product.product_id where cart_id in ($cart_id)");
    }

    /**
     * 购物车商品求和
     * @param $user_id
     * @return mixed
     */
    public function ShopCartAllSum($user_id)
    {
        return DB::select("select sum(sc_product.product_price*sc_cart.number*sc_product.piece_weight) as price,sum(sc_product.piece_weight*sc_cart.number) as weight,sum(sc_cart.number) as num from sc_cart left join sc_product on sc_cart.product_id=sc_product.product_id where user_id= $user_id");
    }


    /**
     * 购物车商品所属仓库
     * @param $user_id
     * @return mixed
     */
    public function GetShopUserCartStorage($user_id)
    {
        return  DB::table($this->table)
            ->select('product.storage','storage.storage_name','storage.storage_city','storage.storage_address','storage.storage_phone')
            ->leftjoin('product','product.product_id', '=', 'cart.product_id')
            ->leftjoin('storage','product.storage', '=', 'storage.storage_id')
            ->where('user_id',$user_id)
            ->groupBy('product.storage')
            ->get();
    }

    /**
     * 根据用户查询购物车数量
     * @param $product_id
     * @param $user_id
     * @return mixed
     * @author alice
     * @date 2016-9-6
     */
    public function APPUserId($user_id)
    {
        return DB::table($this->table)
            ->select('cart.number')
            ->where(['user_id' => $user_id])
            ->get();
    }




    /**
     * App获取购物车列表
     * @param $user_id
     * @return mixed
     */
    // public function GetUserCartList($user_id,$paramObj)
    // {
    //     $offset = ($paramObj->page-1) * HostEnum::PAGE_NUM;
    //     return  DB::table($this->table)
    //             ->select('cart.cart_id','cart.supplier_id','cart.supplier_name','cart.product_id','cart.product_name','cart.product_img','cart.spec_name','cart.price','cart.number','product.stock','cart.place','cart.storage','cart.unit')
    //             ->leftjoin('product','product.product_id', '=', 'cart.product_id')
    //             ->where('user_id',$user_id)
    //             ->skip($offset)->take(HostEnum::PAGE_NUM)
    //             ->orderBy('cart.created','desc')
    //             ->get();
    // }

    public function AppGetUserCartList($user_id)
    {
        return  DB::table($this->table)
            ->select('cart.cart_id','cart.number','brand_name','m_name','product.piece_weight','product.product_price','product.product_spec','storage.storage_name','factory.factory_name')
            ->leftjoin('product','product.product_id', '=', 'cart.product_id')
            ->leftjoin('brand','product.product_brand', '=', 'brand.brand_id')
            ->leftjoin('material','product.product_material', '=', 'material.m_id')
            ->leftjoin('factory','product.factory', '=', 'factory.factory_id')
            ->leftjoin('storage','product.storage', '=', 'storage.storage_id')
            ->where('user_id',$user_id)
            ->orderBy('cart.created','desc')
            ->get();
    }
    /**
     * App删除购物车
     * @param $user_id
     * @param $cart_id
     * @return mixed
     */
    public function DeleteCarte($user_id,$cart_id)
    {
        return  DB::table($this->table)
            ->where('cart_id', $cart_id)
            ->where("user_id", $user_id)
            ->delete();
    }

    /**
     * App购物车one
     * @param $user_id
     * @param $cart_id
     * @return mixed
     */
    public function Carteone($user_id,$cart_id)
    {
        return  DB::table($this->table)
            ->select('product_id','number')
            ->where('cart_id', $cart_id)
            ->where("user_id", $user_id)
            ->first();
    }

    /**
     * APP获取购物车详情
     * @return mixed
     */
    public function APPGetCartone($cart_id)
    {
        return  DB::table($this->table)
            ->select('cart.cart_id','cart.number','brand_name','m_name','product.piece_weight','product.product_price','product.product_spec','storage.storage_name','factory.factory_name')
            ->leftjoin('product','product.product_id', '=', 'cart.product_id')
            ->leftjoin('brand','product.product_brand', '=', 'brand.brand_id')
            ->leftjoin('material','product.product_material', '=', 'material.m_id')
            ->leftjoin('factory','product.factory', '=', 'factory.factory_id')
            ->leftjoin('storage','product.storage', '=', 'storage.storage_id')
            ->where('cart_id',$cart_id)
            ->first();
    }
    /**
     * 查询是否存在商品
     * @param $product_id
     * @param $user_id
     * @return mixed
     * @author alice
     * @date 2016-9-6
     */
    public function APProductId($product_id, $user_id)
    {
        return DB::table($this->table)
            ->where(['product_id'=>$product_id, 'user_id' => $user_id])
            ->first();
    }


    /**
     * 购物车详情页面
     * @param $product_id
     * @param $user_id
     * @return mixed
     * @author alice
     * @date 2016-9-6
     */
    public function APPGetCart($arr,$user_id)
    {
        return  DB::table($this->table)
            ->select('cart.cart_id','cart.number','brand_name','m_name','product.piece_weight','product.product_price','product.product_spec','storage.storage_name','storage.storage_city','storage.storage_phone','factory.factory_name')
            ->leftjoin('product','product.product_id', '=', 'cart.product_id')
            ->leftjoin('brand','product.product_brand', '=', 'brand.brand_id')
            ->leftjoin('material','product.product_material', '=', 'material.m_id')
            ->leftjoin('factory','product.factory', '=', 'factory.factory_id')
            ->leftjoin('storage','product.storage', '=', 'storage.storage_id')
            ->whereIn('cart.cart_id',$arr)
            ->where("user_id", $user_id)
            ->get();
    }
    /**
     * 确认订单商品
     * @param $cart_id
     * @return mixed
     */
    public function APPCartProduct($cart_id)
    {
        return DB::table($this->table)
            ->select('cart.cart_id','cart.number','brand.brand_name','material.m_name','product.piece_weight','product.product_price','storage.storage_name','factory.factory_name','product.product_id')
            ->leftjoin('product','product.product_id', '=', 'cart.product_id')
            ->leftjoin('brand','product.product_brand', '=', 'brand.brand_id')
            ->leftjoin('material','product.product_material', '=', 'material.m_id')
            ->leftjoin('factory','product.factory', '=', 'factory.factory_id')
            ->leftjoin('storage','product.storage', '=', 'storage.storage_id')
            ->whereIn('cart_id',$cart_id)
            ->first();

    }
    /**
     * 全选删除
     * @param $product_id
     * @param $user_id
     * @return mixed
     * @author alice
     * @date 2016-9-6
     */
    public function APdelect($user_id)
    {
        return DB::table($this->table)
            //->whereIn('cart_id',$cart_id)
            ->where("user_id", $user_id)
            ->delete();
    }

    /**
     * 全选删除
     * @param $product_id
     * @param $user_id
     * @return mixed
     * @author alice
     * @date 2016-9-6
     */
    public function APdelecte($cart_id,$user_id)
    {
        return DB::table($this->table)
            ->whereIn('cart_id',$cart_id)
            ->where("user_id", $user_id)
            ->delete();
    }









    /*****************APP接口结束**********************************/













    /**
     * PC获取购物车列表
     * @param $user_id
     * @return mixed
     */
    public function GetShopUserCartList($user_id,$storage)
    {
        return  DB::table($this->table)
            ->select('cart.cart_id','cart.number','brand_name','m_name','product.piece_weight','product.product_spec','product.product_id','product.product_price','storage.storage_name','product.useable_stock')
            ->leftjoin('product','product.product_id', '=', 'cart.product_id')
            ->leftjoin('brand','product.product_brand', '=', 'brand.brand_id')
            ->leftjoin('material','product.product_material', '=', 'material.m_id')
            ->leftjoin('storage','product.storage', '=', 'storage.storage_id')
            ->where('user_id',$user_id)
            ->where('storage',$storage)
            ->orderBy('cart.created','desc')
            ->get();
    }



    /**
     * PC获取购物车商品个数
     * @param $user_id
     * @return mixed
     */
    public function ShopCartCount($user_id)
    {
        return  DB::table($this->table)
            ->select('cart.cart_id')
            ->where('user_id',$user_id)
            ->count();
    }

    /**
     * PC获取购物车商品ID
     * @param $user_id
     * @return mixed
     */
    public function GetCartId($user_id)
    {
        return  DB::table($this->table)
            ->select('cart_id')
            ->where('user_id',$user_id)
            ->get();
    }


    /**
     * 获取订单详情
     * @return mixed
     */
    public function GetCartDetail($cart_id)
    {
        return  DB::table($this->table)
            ->select('cart.cart_id','cart.number','brand.brand_name','material.m_name','product.piece_weight','product.product_id','product.product_spec','product.useable_stock','product.product_price','storage.storage_name','factory.factory_name','storage.storage_place','storage.storage_address','storage.storage_phone')
            ->leftjoin('product','product.product_id', '=', 'cart.product_id')
            ->leftjoin('brand','product.product_brand', '=', 'brand.brand_id')
            ->leftjoin('material','product.product_material', '=', 'material.m_id')
            ->leftjoin('factory','product.factory', '=', 'factory.factory_id')
            ->leftjoin('storage','product.storage', '=', 'storage.storage_id')
            ->where('cart_id',$cart_id)
            ->first();
    }


    /**
     * 删除购物车
     * @param $user_id
     * @param $cart_id
     * @return mixed
     */
    public function DeleteCart($user_id,$cart_id)
    {
        return  DB::table($this->table)
            ->where('cart_id', $cart_id)
            ->where("user_id", $user_id)
            ->delete();
    }

    /**
     * 查询是否存在商品
     * @param $product_id
     * @param $user_id
     * @return mixed
     * @author alice
     * @date 2016-2-21
     */
    public function GetProductId($product_id, $user_id)
    {
        return DB::table($this->table)
            ->where(['product_id'=>$product_id, 'user_id' => $user_id])
            ->first();
    }

    public function GetCartList($product_id,$user_id)
    {
        return DB::table($this->table)
                ->select('supplier_id','supplier_name','product_id','goods_thumb','early_pay','insurance','is_early_pay','is_insurance')
                ->whereIn('product_id',$product_id)
                ->where('user_id',$user_id)
                ->get();
    }

    /**
     * 购物车信息
     * @param $cart_id
     * @return mixed
     * @author fyl
     * @date 2016-08-03
     */
    public function GetCartInfo($cart_id)
    {
        return DB::table($this->table)
                ->select('product_id','supplier_id')
                ->whereIn('cart_id',$cart_id)
                ->get();
    }

    /**
     * 获取购物车详情
     * @param $cart_id
     * @return mixed
     */
    public function GetCartProducts($cart_id)
    {
        return DB::table($this->table)
            ->select('cart.cart_id','cart.number','brand.brand_name','material.m_name','product.piece_weight','product.product_spec','product.useable_stock','product.product_price','storage.storage_name','factory.factory_name','storage.storage_place','storage.storage_address','storage.storage_phone')
            ->leftjoin('product','product.product_id', '=', 'cart.product_id')
            ->leftjoin('brand','product.product_brand', '=', 'brand.brand_id')
            ->leftjoin('material','product.product_material', '=', 'material.m_id')
            ->leftjoin('factory','product.factory', '=', 'factory.factory_id')
            ->leftjoin('storage','product.storage', '=', 'storage.storage_id')
            ->whereIn('cart_id',$cart_id)
            ->get();
    }

    public function GetCartAmount($user_id,$supplier_id)
    {
       return DB::table($this->table)
            ->where(['user_id' => $user_id, 'supplier_id' => $supplier_id ])
            ->sum('amount');
    }


    /**
     * 购物车信息
     * @param $user_id
     * @return mixed
     * @date 2016-7-02
     */
    public function GetShopCartId($user_id)
    {
        return DB::table($this->table)
            ->select('cart_id')
            ->where('user_id',$user_id)
            ->get();
    }


    /**
     * 删除购物车
     * @param $cart_id
     * @return mixed
     */
    public function ShopCartDeleteAll($cart_id)
    {
        return  DB::table($this->table)
            ->whereIn('cart_id', $cart_id)
            ->delete();
    }




}